package com.zingrow.web.report.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Splitter;
import com.zingrow.common.utils.PageView;
import com.zingrow.web.base.service.impl.BaseService;
import com.zingrow.web.customer.mapper.GroupMapper;
import com.zingrow.web.report.mapper.RptModelMapper;
import com.zingrow.web.report.mapper.RptModuleInfoMapper;
import com.zingrow.web.report.model.RptModel;
import com.zingrow.web.report.model.RptModule;
import com.zingrow.web.report.model.RptModuleInfo;
import com.zingrow.web.report.response.ModuleData;
import com.zingrow.web.report.service.IRptModelService;
import com.zingrow.web.report.service.IRptModuleService;

@Service
public class RptModelServiceImpl extends BaseService<ModuleData> implements IRptModelService{

	@Autowired
	private RptModelMapper rptModelMapper;
	@Autowired
	private RptModuleInfoMapper rptModuleInfoMapper;
	@Autowired
	private IRptModuleService iRptModuleService;
	@Autowired
	private GroupMapper groupMapper;
	
	@Override
	public void initMapper() {
	}
	
	@Override
	public List<RptModel> selectAll(PageView pageView, String name,
			String note, Integer groupId, Integer cycleId,Integer isOpen) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		if(null!=pageView)map.put("pageView", pageView);
		if(null!=name)map.put("name", name);
		if(null!=note)map.put("note", note);
		if(null!=groupId)map.put("groupId", groupId);
		if(null!=cycleId)map.put("cycleId", cycleId);
		if(null!=isOpen)map.put("isOpen", isOpen);
		List<RptModel> list = rptModelMapper.selectAll(map);
		return list;
	}
	/*@Transactional
	@Override
	public boolean insert2(RptModel rptModel,String moduleId,List<String> ifmId) {
		RptModuleInfo moduleInfo = new RptModuleInfo();
		RptView view = new RptView();
		//新增报告模板数据
		if(rptModel.getName().equals("定期日报")){
			rptModel.setCycleId(1);
		}else if(rptModel.getName().equals("定期周报")){
			rptModel.setCycleId(2);
		}else if(rptModel.getName().equals("定期月报")){
			rptModel.setCycleId(3);
		}
		int count = rptModelMapper.insert(rptModel);
		//根据逗号分割，得到模块ID集合
		List<String> idAll = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(moduleId);
		for (String ids : idAll) {
			if(ids.equals(23)||ids.equals(16)||ids.equals(1)||ids.equals(13)){
				//1代表文字展示类型
				view.setViewTypeId(1);
				if(ids.equals(23)){
					view.setName("重点新闻排行前十");view.setTitle("重点新闻排行前十");
				}else if(ids.equals(16)){
					view.setName("新闻数据明细");view.setTitle("新闻数据明细");
				}else if(ids.equals(1)){
					view.setName("新闻舆情概述");view.setTitle("新闻舆情概述");
				}else if(ids.equals(13)){
					view.setName("舆情概述");view.setTitle("舆情概述");
				}
				//新增展示信息表数据
				rptViewMapper.insert(view);
			}else if(ids.equals(7)||ids.equals(14)||ids.equals(18)||ids.equals(19)
					||ids.equals(9)||ids.equals(15)||ids.equals(17)){
				//2代表表格展示类型
				view.setViewTypeId(2);
				if(ids.equals(7)){
					view.setName("新闻排行");view.setTitle("新闻排行");
				}else if(ids.equals(14)){
					view.setName("敏感信息列表");view.setTitle("敏感信息列表");
				}else if(ids.equals(18)){
					view.setName("微博数据");view.setTitle("微博数据");
				}else if(ids.equals(19)){
					view.setName("论坛数据");view.setTitle("论坛数据");
				}else if(ids.equals(17)){
					view.setName("微信数据");view.setTitle("微信数据");
				}else if(ids.equals(9)){
					view.setName("一周数据总览");view.setTitle("一周数据总览");
				}else if(ids.equals(15)){
					view.setName("新闻数据");view.setTitle("新闻数据");
				}
				//新增展示信息表数据
				rptViewMapper.insert(view);
			}else if(ids.equals(3)||ids.equals(4)||ids.equals(20)||ids.equals(22)
					||ids.equals(21)||ids.equals(5)){
				//3代表展示类型饼图
				view.setViewTypeId(3);
				if(ids.equals(3)){
					view.setName("舆情份额");view.setTitle("舆情份额");
				}else if(ids.equals(4)){
					view.setName("总体敏感信息占比");view.setTitle("总体敏感信息占比");
				}else if(ids.equals(20)){
					view.setName("微信敏感信息占比");view.setTitle("微信敏感信息占比");
				}else if(ids.equals(22)){
					view.setName("论坛敏感信息占比");view.setTitle("论坛敏感信息占比");
				}else if(ids.equals(21)){
					view.setName("微博敏感信息占比");view.setTitle("微博敏感信息占比");
				}else if(ids.equals(5)){
					view.setName("新闻敏感信息占比");view.setTitle("新闻敏感信息占比");
				}
				//新增展示信息表数据
				rptViewMapper.insert(view);
			}else if(ids.equals(6)||ids.equals(10)||ids.equals(8)||ids.equals(12)){
				//4代表展示类型柱状图
				view.setViewTypeId(4);
				if(ids.equals(6)){
					view.setName("媒体发稿Top10");view.setTitle("媒体发稿Top10");
				}else if(ids.equals(10)){
					view.setName("一周数据总览表");view.setTitle("一周数据总览表");
				}else if(ids.equals(8)){
					view.setName("舆情信息对比图");view.setTitle("舆情信息对比图");
				}else if(ids.equals(12)){
					view.setName("自媒体敏感信息对比图");view.setTitle("自媒体敏感信息对比图");
				}
				//新增展示信息表数据
				rptViewMapper.insert(view);
			}
			//把报告模板ID加入到模板模块对象
			moduleInfo.setModelId(rptModel.getId());
			//把展示信息表ID加入到模板模块对象
			moduleInfo.setViewInfoId(view.getId());
			for (String sid : idAll) {
				for (String ifmids : ifmId) {
					List<String> ifmidAll = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(ifmids);
					for (String sifmid : ifmidAll) {
						//根据方案ID和组织ID查询该方案是否为敏感
						GroupScheme groupScheme = schemeMapper.selectgidfid(rptModel.getGroupId(), Integer.valueOf(sifmid));
						//方案ID
						moduleInfo.setPlanId(Integer.valueOf(sifmid));
						//模块ID
						moduleInfo.setModuleId(Integer.valueOf(sid));
						//方案是否敏感
						moduleInfo.setIsSensitive(groupScheme.getIfmIsSensitivity());
						//新增模板模块记录表
						rptModuleInfoMapper.insert(moduleInfo);
					}
				}
			}
		}
		return count > 0;
	}*/
	//查询定期报告模板和周期
	@Override
	public List<RptModel> select() {
		List<RptModel> modellist = rptModelMapper.select();
		for (RptModel rptModel : modellist) {
			List<String> list = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(rptModel.getModuleSort());
			List<RptModule> modulelist = new ArrayList<RptModule>();
			for (String slist : list) {
				RptModule module= rptModuleInfoMapper.selectmoduleid(Integer.valueOf(slist));
				if (null!=module) {
					modulelist.add(module);
				}
			}
			rptModel.setModulelist(modulelist);
		}
		return modellist;
	}
	
	//新增
	@Override
	public boolean insert(RptModel rptModel,List<RptModuleInfo> mlist) {
		RptModel model = new RptModel();
		//根据对象中的模板ID查询
		model = rptModelMapper.selectmodelId(rptModel.getId());
		model.setGroupId(rptModel.getGroupId());
		model.setIsOpen(0);
		int count = rptModelMapper.insert(model);
		//根据模板ID查询数据库中已模拟好的数据
		List<RptModuleInfo> modulelist = rptModuleInfoMapper.selectBymodelid(rptModel.getId());
		for (RptModuleInfo module : modulelist) {
			for (RptModuleInfo rpt : mlist) {
				if (module.getModuleId()==rpt.getModuleId()) {
					rpt.setModelId(model.getId());
					rpt.setViewInfoId(module.getViewInfoId());
					rpt.setDimensionId(module.getDimensionId());
					rptModuleInfoMapper.insert(rpt);
				}
			}
		}
		/*group.setGroupId(model.getGroupId()); //新增组织的ID
		group.setRptRight(1);  //该组织新增了报告，该组织设置为拥有报告权限
		//报告模板新增成功后，设置该组织的报告权限为开启
		groupMapper.updateByGroupId(group);*/
		return count>0;
	}
	
	//删除
	@Override
	@Transactional
	public boolean del(Integer id) {
		//根据模板ID删除模板模块表
		rptModuleInfoMapper.deleteByPrimaryKey(id);
		//根据模板ID删除模板
		return rptModelMapper.deleteByPrimaryKey(id)>0;
	}
	
	//修改
	@Override
	@Transactional
	public boolean update(RptModel rptModel,List<RptModuleInfo> module,Integer id) {
		int count = 0;
		//根据数据库模板数据ID查询模块数据
		List<RptModuleInfo> listmodule = rptModuleInfoMapper.selectBymodelid(id);
		//根据模板ID删除模板模块表数据
		rptModuleInfoMapper.deleteByPrimaryKey(rptModel.getId());
		if (listmodule.size()>0) {
			for (RptModuleInfo rptModuleInfo : listmodule) {
				for (RptModuleInfo rptmodule : module) {
					if (rptModuleInfo.getModuleId()==rptmodule.getModuleId()) {
						rptModuleInfo.setPlanId(rptmodule.getPlanId());
						rptModuleInfo.setIsSensitive(rptmodule.getIsSensitive());
						rptModuleInfo.setModelId(rptModel.getId());
						//新增数据
						count = rptModuleInfoMapper.insert(rptModuleInfo);
					}
				}
			}
		}
		return count > 0;
	}
	
	//开启关闭
	@Override
	public boolean isOpen(RptModel model) {
		return rptModelMapper.update(model)>0;
	}

	@Override
	public List<RptModel> selectModelgroupId(Integer groupId,Integer planId) {
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("groupId", groupId);
		map.put("planId", planId);
		return rptModelMapper.selectModelgroupId(map);
	}

	@Override
	public List<RptModel> queryByGroupId(PageView pageView,String modelNamem,String modelNote,Integer groupId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		map.put("modelName", modelNamem);
		map.put("modelNote", modelNote);
		map.put("groupId", groupId);
		return rptModelMapper.queryByGroupId(map);
	}

	@Override
	public int queryByGidCid(Integer groupId, Integer cycleId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("groupId", groupId);
		map.put("cycleId", cycleId);
		return rptModelMapper.queryByGidCid(map);
	}
	@Override
	public RptModel selectByPrimaryKey(Integer id)
	{
		return rptModelMapper.selectByPrimaryKey(id);
	};
	
}
